package cn.sinata.wanglin.village.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.fragment.RecyclerViewFragment;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.TimeUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import cn.sinata.wanglin.adapter.MessageAdapter;
import cn.sinata.wanglin.adapter.NewsinfoAdapter;
import cn.sinata.wanglin.adapter.NoticeAdapter;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Active;
import cn.sinata.wanglin.model.Message;
import cn.sinata.wanglin.model.Newsinfo;
import cn.sinata.wanglin.model.NoticeMessage;
import cn.sinata.wanglin.model.SameCityClass;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.RelativeDateFormat;
import cn.sinata.wanglin.village.activity.NewsinfoDetailsActivity;

/**
 *小区---新闻资讯的list.
 *
 */
public class NewsinfoListFragment extends RecyclerViewFragment {

    private BaseRecyclerAdapter mNewsinfoAdapter;
    private List<Newsinfo> mNewsinfos = new ArrayList<>();
    private int type;
    private int pageNo = 0;
    //排序（0全部 默认排序，1热议 排序，2原创）
    public static NewsinfoListFragment newInstance(int type){
        NewsinfoListFragment mNewsinfoListFragment = new NewsinfoListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type",(type+1));
        mNewsinfoListFragment.setArguments(bundle);
        return mNewsinfoListFragment;
    }

    @Override
    protected RecyclerView.Adapter setAdapter() {
        if (getArguments()!=null){
            type = getArguments().getInt("type");
            Utils.systemErr("---->"+type);
        }
        mNewsinfoAdapter = new NewsinfoAdapter(mNewsinfos);
        mNewsinfoAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
               Newsinfo mNewsinfo= mNewsinfos.get(position);
               ActivityUtil.create(NewsinfoListFragment.this).go(NewsinfoDetailsActivity.class)
                        .put("id",mNewsinfo.getId())
                        .start();
            }
        });
        return mNewsinfoAdapter;
    }

    @Override
    protected void onFirstVisibleToUser() {
        setEmptyViewClick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
                pullDownRefresh();
            }
        });
        if (getArguments()!=null){
            type = getArguments().getInt("type");
        }
        setItemDecoration(DEFAULT_MARGIN_DIVIDER);
        getData(""+type);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode == Activity.RESULT_OK){
//            if (requestCode == 0 && data!=null){
//                int position = data.getIntExtra("position",-1);
//                if (position>-1 && position<messages.size()){
//                    messages.get(position).setRead(1);
//                    messageAdapter.notifyDataSetChanged();
//                }
//            }
//        }
    }

    @Override
    protected void onVisibleToUser() {

    }

    @Override
    protected void onInvisibleToUser() {

    }

    @Override
    protected void pullDownRefresh() {
        super.pullDownRefresh();
        pageNo = 0;
        getData(""+type);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        pageNo++;
        getData(""+type);
    }
    private void getData(String sort) {
        HttpManager.getNews(sort,pageNo)
                .subscribe(new ResultDataSubscriber<ArrayList<Newsinfo>>(this) {
                    @Override
                    public void onSuccess(String msg, ArrayList<Newsinfo> data) {
                        if (pageNo == 0) {
                            mNewsinfos.clear();
                        }
                        if (data.size() == 0) {
                            if (pageNo == 0) {
                                setLoadMoreText("暂无数据");
                            } else {
                                pageNo --;
                                setLoadMoreText("没有更多了");
                            }
                        }
                        mNewsinfos.addAll(data);
                        mNewsinfoAdapter.notifyDataSetChanged();
                    }
                });

    }

}
